package com.doublethread.easymart.modules.cart.controller;

import com.doublethread.easymart.common.context.UserContext;
import com.doublethread.easymart.common.result.Result;
import com.doublethread.easymart.modules.cart.dto.CartAddDTO;
import com.doublethread.easymart.modules.cart.dto.CartUpdateDTO;
import com.doublethread.easymart.modules.cart.service.CartService;
import com.doublethread.easymart.modules.cart.vo.CartVO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/user/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 获取用户购物车列表
     */
    @GetMapping
    public Result<List<CartVO>> getUserCartList() {
        Long userId = UserContext.getCurrentUserId();

        log.info("获取用户购物车列表");

        List<CartVO> cartList = cartService.getUserCartList(userId);

        log.info("获取用户购物车列表成功");
        return Result.success(cartList);
    }

    /**
     * 添加商品到购物车
     */
    @PostMapping
    public Result<Void> addToCart(@Valid @RequestBody CartAddDTO cartAddDTO) {
        Long userId = UserContext.getCurrentUserId();

        log.info("添加商品到购物车");

        cartService.addToCart(userId, cartAddDTO);

        log.info("添加商品到购物车成功");
        return Result.success();
    }

    /**
     * 更新购物车商品数量
     */
    @PutMapping("/{id}")
    public Result<Void> updateCartItem(@PathVariable Long id,
                                       @Valid @RequestBody CartUpdateDTO cartUpdateDTO) {
        Long userId = UserContext.getCurrentUserId();

        log.info("更新购物车商品数量");

        cartService.updateCartItem(userId, id, cartUpdateDTO);

        log.info("更新购物车商品数量成功");
        return Result.success();
    }

    /**
     * 删除购物车商品
     */
    @DeleteMapping("/{id}")
    public Result<Void> removeCartItem(@PathVariable Long id) {
        Long userId = UserContext.getCurrentUserId();

        log.info("删除购物车商品");

        cartService.removeCartItem(userId, id);

        log.info("删除购物车商品成功");
        return Result.success();
    }

    /**
     * 批量删除购物车商品
     */
    @DeleteMapping("/batch")
    public Result<Void> removeCartItems(@RequestBody List<Long> cartIds) {
        Long userId = UserContext.getCurrentUserId();

        log.info("批量删除购物车商品");

        cartService.removeCartItems(userId, cartIds);

        log.info("批量删除购物车商品成功");
        return Result.success();
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/clear")
    public Result<Void> clearUserCart() {
        Long userId = UserContext.getCurrentUserId();

        log.info("清空购物车");

        cartService.clearUserCart(userId);

        log.info("清空购物车成功");
        return Result.success();
    }

    /**
     * 获取购物车商品总数量
     */
    @GetMapping("/count")
    public Result<Integer> getCartItemCount() {
        Long userId = UserContext.getCurrentUserId();

        log.info("获取购物车商品总数量");

        Integer count = cartService.getCartItemCount(userId);

        log.info("获取购物车商品总数量成功");
        return Result.success(count);
    }

    /**
     * 检查购物车商品库存
     */
    @PostMapping("/check-stock")
    public Result<Void> checkCartStock(@RequestBody List<Long> cartIds) {
        Long userId = UserContext.getCurrentUserId();

        log.info("检查购物车商品库存");

        cartService.checkCartStock(userId, cartIds);

        log.info("检查购物车商品库存成功");
        return Result.success();
    }
}
